var ckdcx = {
    setBtnStatus: function () {
        var row = $('#ckdcx-grid').datagrid('getSelected');
        if (row.sftzd == 1) {
            $("#btn_ckdcx_confirm").hide();
        } else if (row.sftzd == 0) {
            $("#btn_ckdcx_confirm").show();
        }
    }
}
$(function () {
    //表格初始加载
    $('#ckdcx-grid').datagrid({
        url: $.webPath() + "/jggl/jgdgl/ckdcxList",
        toolbar: "#ckdcx-toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        showFooter: true,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'id', title: 'id', hidden: true},
                {field: 'jgdmxid', title: 'jgdmxid', hidden: true},
                {field: 'cxdh', title: '冲销单号', align: 'center', width: 100},
                {field: 'ckdh', title: '出库单号', align: 'center', width: 100},
                {field: 'khbm', title: '卡号编码', align: 'center', width: 100},
                {field: 'code', title: '物资编码', align: 'center', width: 70},
                {field: 'name', title: '物资名称', align: 'center', width: 50},
                {field: 'ggxh', title: '规格型号', align: 'center', width: 70},
                {field: 'dw', title: '单位', align: 'center', width: 50},
                {field: 'cd', title: '产地', align: 'center', width: 50},
                {field: 'mc', title: '仓库', align: 'center', width: 50},
                {field: 'sku', title: '标识', align: 'center', width: 65},
                {field: 'jgsl', title: '冲销数量', align: 'center', width: 40},
                {field: 'jgzl', title: '冲销重量', align: 'center', width: 50},
                {field: "sftzd", title: "状态", align: 'center', width: 47,
                    formatter: function (value) {
                        if (value == 0)
                            return '已保存';
                        if (value == 1)
                            return '已审核';
                    }
                }
            ]
        ],
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
            ckdcx.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow: function () {
            ckdcx.setBtnStatus();
        },
        onSelect: function (rowIndex, rowData) {
            ckdcx.setBtnStatus();
        }
    });

    //审核按钮
    $("#btn_ckdcx_confirm").click(function () {
        ckdcx_check();
    });
    //查询按钮
    $("#btn_ckdcx_search").click(function () {
        ckdcx_search();
    });
});

//审核
function ckdcx_check() {
    var row = $('#ckdcx-grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否对该冲销单进行审核？该冲销单冲销量为：' + row.jgzl + '立方米(m³)', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/jggl/jgdgl/chang_ckdcx",
                    type: "post",
                    dataType: 'text',
                    data: {ckdId: row.id, jgdmx_id: row.jgdmxid, cxsl: row.jgsl, cxzl: row.jgzl},
                    success: function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.statusCode == '200') {
                            $('#ckdcx-grid').datagrid('reload');
                            $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
                        } else {
                            $('#ckdcx-grid').datagrid('reload');
                            $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
                        }
                    },
                    error: function (data) {
                        alert(data);
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要审核的冲销单！", "warning");
    }
}

//加工单查询
function ckdcx_search() {
    var ckdcx_ckdh = $("#ckdcx-toolbar input[name='ckdcx_ckdh']").val();
    var ckdcx_khbm = $("#ckdcx-toolbar input[name='ckdcx_khbm']").val();
    var sftzd = $("#ckdcx-toolbar input[name='sftzd']").val();
    var cxdh = $("#ckdcx-toolbar input[name='ckdcx_cxdh']").val();
    $('#ckdcx-grid').datagrid('load',
        {
            ckdh: ckdcx_ckdh,
            khbm: ckdcx_khbm,
            sftzd: sftzd,
            cxdh:cxdh
        }
    );
}
